﻿using Microsoft.EntityFrameworkCore;
using System.Diagnostics;
using WidgetsDesign.API.DataBase;
using WidgetsDesign.API.Models;

namespace WidgetsDesign.API.Services
{
    public class MeetingRepository : IMeetingRepository
    {
        private readonly AppDbContext _context;
        public MeetingRepository(AppDbContext context)
        {
            _context = context;
        }
        public void CreateTrain(MeetingModel model)
        {
            _context.Add(model);
        }
        public void UpdateTrain(MeetingModel model)
        {
            var query = (from h in _context.meeting
                         where h.MeetingId == model.MeetingId
                         select h);
            var info = query.FirstOrDefault();
            if (info != null)
            {
                info.Name = model.Name;
            }
        }

        public void UpdateEmployees(MeetingModel model)
        {
            var query = (from h in _context.meeting
                         where h.MeetingId == model.MeetingId
                         select h);
            var info = query.FirstOrDefault();
            if (info != null)
            {
                info.Employees = model.Employees;
                info.State = 2;
            }
        }
        public async Task<List<MeetingModel>> queryList(int pageIndex, int pageSize, DateTime? startTime = null, DateTime? endTime = null, String? name = null)
        {
            IQueryable<MeetingModel> query = _context.meeting;
            if (startTime.HasValue)
            {
                query = query.Where(h => h.CreateTime >= startTime);
            }

            if (endTime.HasValue)
            {
                query = query.Where(h => h.CreateTime <= endTime);
            }
            if (name != null)
            {
                query = query.Where(h => h.Name.Contains(name));
            }

            //按时间排序
            query = query.OrderByDescending(h => h.CreateTime);

            query = query.Where(h => h.State == 5)
                      .Skip(pageSize * (pageIndex - 1))
                     .Take(pageSize);

            return await query.ToListAsync();
        }

        /// <summary>
        /// 获取总数
        /// </summary>
        /// <returns></returns>
        public async Task<int> getTotal(DateTime? startTime = null, DateTime? endTime = null)
        {
            IQueryable<MeetingModel> query = _context.meeting;
            if (startTime.HasValue)
            {
                query = query.Where(h => h.CreateTime >= startTime);
            }

            if (endTime.HasValue)
            {
                query = query.Where(h => h.CreateTime <= endTime);
            }

            query = query.Where(h => h.State == 5);

            return await query.CountAsync();
        }
        public async Task<MeetingModel> UnderwayTrain()
        {
            var query = (from h in _context.meeting where h.State != 5 select h);
            var list = await query.FirstOrDefaultAsync();
            if (list != null)
            {
                return list;
            }
            return new MeetingModel { };
        }

        public void UpdetaFiles(string fileJson)
        {
            var query = (from h in _context.meeting where h.State != 5 select h);
            var list = query.FirstOrDefault();
            list.Files = fileJson;
            _context.SaveChanges();
        }
        public async Task<MeetingModel> queryTrainById(string id)
        {
            var query = (from h in _context.meeting where h.MeetingId == id select h);
            return await query.FirstOrDefaultAsync();
        }
        public void SaveVideo(string id)
        {
            var query = (from h in _context.meeting where h.MeetingId == id select h);
            var list = query.FirstOrDefault();
            if (list != null)
            {
                list.State = 5;
            }
            _context.SaveChanges();
        }
        /// <summary>
        /// 删除state != 5 的
        /// </summary>
        public void DeleteErrorTrain()
        {
            var query = (from h in _context.meeting where h.State != 5 select h);
            _context.meeting.RemoveRange(query);

        }
        /// <summary>
        /// 保存
        /// </summary>
        /// <returns></returns>
        public async Task<bool> SaveAsync()
        {
            return (await _context.SaveChangesAsync() >= 0);
        }

        public void DeleteTrainById(string id)
        {
            var query = (from h in _context.meeting where h.MeetingId == id select h);
            _context.meeting.RemoveRange(query);
            _context.SaveChanges();
        }
    }
}
